{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Model Quickload\n",
    "\n",
    "This short notebook will show you how you can very quickly load and test SqueezeNet, which is a very small and fast model based on AlexNet and is useful for identifying objects. The range of objects groups is only 1,000.\n",
    "\n",
    "Before this script will work, you need to download the model and install it. You can do this by running:\n",
    "\n",
    "```\n",
    "sudo python -m caffe2.python.models.download -i squeezenet\n",
    "```\n",
    "\n",
    "Or make a squeezenet folder, download each file listed below to it, and place it in `/caffe2/python/models/`:\n",
    "* [predict_net.pb](https://download.caffe2.ai/models/squeezenet/predict_net.pb)\n",
    "* [init_net.pb](https://download.caffe2.ai/models/squeezenet/init_net.pb)\n",
    "\n",
    "The helper functions will look up the top object detection result for you by searching through this [inference codes file](inference_codes.txt). If you want to see how well the model detects samples different than provided here, take a look at the codes, and find an image url to an image of an object in the list of codes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Raw top 3 results: [array([985.0, 0.9822268486022949], dtype=object), array([309.0, 0.011943698860704899], dtype=object), array([946.0, 0.004810151644051075], dtype=object)]\n",
      "The image contains a daisy with a 98.2226848602 percent probability.\n"
     ]
    }
   ],
   "source": [
    "# load up the caffe2 workspace\n",
    "from caffe2.python import workspace\n",
    "# choose your model here (use the downloader first)\n",
    "from caffe2.python.models import squeezenet as mynet\n",
    "# helper image processing functions\n",
    "import helpers\n",
    "\n",
    "# load the pre-trained model\n",
    "init_net = mynet.init_net\n",
    "predict_net = mynet.predict_net\n",
    "# you must name it something\n",
    "predict_net.name = \"squeezenet_predict\"\n",
    "workspace.RunNetOnce(init_net)\n",
    "workspace.CreateNet(predict_net)\n",
    "p = workspace.Predictor(init_net.SerializeToString(), predict_net.SerializeToString())\n",
    "\n",
    "# use whatever image you want (urls work too)\n",
    "# img = \"https://upload.wikimedia.org/wikipedia/commons/a/ac/Pretzel.jpg\"\n",
    "# img = \"images/cat.jpg\"\n",
    "# img = \"images/cowboy-hat.jpg\"\n",
    "# img = \"images/cell-tower.jpg\"\n",
    "# img = \"images/Ducreux.jpg\"\n",
    "# img = \"images/pretzel.jpg\"\n",
    "# img = \"images/orangutan.jpg\"\n",
    "# img = \"images/aircraft-carrier.jpg\"\n",
    "img = \"images/flower.jpg\"\n",
    "# average mean to subtract from the image\n",
    "mean = 128\n",
    "# the size of images that the model was trained with\n",
    "input_size = 227\n",
    "\n",
    "# use the image helper to load the image and convert it to NCHW\n",
    "img = helpers.loadToNCHW(img, mean, input_size)\n",
    "\n",
    "# submit the image to net and get a tensor of results\n",
    "results = p.run([img])   \n",
    "response = helpers.parseResults(results)\n",
    "# and lookup our result from the inference list\n",
    "print response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
